#!/usr/bin/env python
#-*-coding:utf-8-*-

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This project is sponsored by:
#   http://www.equiposcreativos.com/

################################################################################
# META
################################################################################

__version__ = "0.2"
__license__ = "GPL3"
__author__ = "JBC"
__since__ = "0.2"
__date__ = "2010/09/05"


################################################################################
# IMPORTS
################################################################################

import re

from lib import feedparser
from lib import BeautifulSoup
from lib.xgoogle import translate

import textutil
import feeds
import stopwords

################################################################################
# CLASSES
################################################################################

class _MemoryTranslator(object):
    
    def __init__(self):
        self.memory = {}
        self._t = translate.Translator()
        self._d = translate.LanguageDetector()
        
    def translate(self, text, to_lang):
        mem = (text, to_lang)
        if mem in self.memory:
            return self.memory[mem]
        else:
            value = self._t.translate(text, lang_to=to_lang)
            self.memory[mem] = value
            return value
        
    def language_of(self, text):
        return self._d.detect(text).lang_code
        

################################################################################
# FUNCTIONS
################################################################################

def _split(text):
    text = " ".join([t.strip()
                     for t in re.split("\W", text) 
                     if t.strip()])
    
    return set([t 
                for t in text.split()
                if t not in stopwords.get_stopwords()])


def _html2text(html):
    soup = BeautifulSoup.BeautifulSoup(html)
    parts = soup.findAll(text=True)
    return " ".join(parts).strip()
    
    

def search(query, toLang, replace={}, agent="rssthief"):
    entries = []
    translator = _MemoryTranslator()
    for feed_url in feeds.get_feeds():
        try:
            feed = feedparser.parse(feed_url, agent=agent)
            
            for entry in feed.entries:
                summary = _html2text(entry.summary)
                summary = textutil.normalize(summary)
                summ_lang = translator.language_of(summary)
                if  summ_lang != toLang:
                    query = translator.translate(query, summ_lang)
                    query = textutil.normalize(query)
                    query = _split(query)
                    summary = _split(summary)
                    if query.intersection(summary):
                            entries.append(entry)
        except:
            continue
    
    for idx, entry in enumerate(entries):
        e = {}
        
        if entry.title_detail.type == "text/plain":
            e["title"] = entry.title_detail.value
        else:
            e["title"] = _html2text(entry.title_detail.value)
            for f, t in replace.items():
                e["title"] = e["title"].replace(f, t)
                
        e["content"] = ""
        try:
            for c in entry.content:
                if c.type == "text/plain":
                    e["content"] += "\n" + c.value
                else:
                    e["content"] += "\n" + _html2text(c.value)
        except AttributeError:
            pass
        else:
            e["content"] = textutil.normalize(e["content"])
            e["content"] = translator.translate(e["content"], toLang)
        finally:
            for f, t in replace.items():
                e["content"] = e["content"].replace(f, t)
        
        e["summary"] = ""
        try:
            if entry.summary_detail.type == "text/plain":
                e["summary"] = entry.summary_detail.value
            else:
                e["summary"] = _html2text(entry.summary_detail.value)
        except AttributeError:
            pass
        else:
            e["summary"] = textutil.normalize(e["summary"])
            e["summary"] = translator.translate(e["summary"], toLang)
        finally:
            for f, t in replace.items():
                e["summary"] = e["summary"].replace(f, t)
        
        e["link"] = getattr(entry, "link", "")
        
        entries[idx] = e
    
    return entries
